'''
= Observable Base Class =

Provides facilities for registering observers and broadcasting events.
'''

class Observable(object):
    '''Observable base class.'''
    
    def __init__(self):
        super(Observable, self).__init__()
        
        self._observers = []
        
    @property
    def observers(self):
        '''All observers.'''
        
        return self._observers[:]
    
    def add_observer(self, x):
        '''Add an observer. The observer must be callable with a single argument.'''
        
        self._observers.append(x)
        
    def remove_observer(self, x):
        '''Remove an observer.'''
        
        self._observers.remove(x)
    
    def clear_observers(self):
        '''Remove all observers.'''
        
        self._observers = []
        
    def broadcast(self, arg):
        '''Call all observers with the given argument as parameter.'''
        
        for x in self.observers:
            x(arg)
